package com.hxwb.chainmaker.rpc.controller;

import com.hxwb.chainmaker.rpc.common.MessageResult;
import com.hxwb.chainmaker.rpc.common.UnitString;
import com.hxwb.chainmaker.rpc.entity.Account;
import com.hxwb.chainmaker.rpc.param.*;
import com.hxwb.chainmaker.rpc.service.ChainmakerService;
import com.hxwb.chainmaker.rpc.util.GeneratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rpc")
public class ChainmakerController {
    @Autowired
    private ChainmakerService chainmakerService;

    /*获取一个新的长安链地址*/
    @GetMapping("/address/{account}")
    public MessageResult getNewAddress(@PathVariable String account) {
        System.out.println("create new account=" + account);
        String UUserId = GeneratorUtil.getUUserId(account);
        try {
            String address;
            address = chainmakerService.createNewWallet(UUserId);
            if (address.equals("ERROR")) {
                MessageResult result = new MessageResult(0, "error");
                result.setData("地址库已用完,请稍后获取！");
                return result;
            }
            MessageResult result = new MessageResult(200, "success");
            result.setData(address);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return MessageResult.error(500, "MAY rpc error:" + e.getMessage());
        }
    }

    /*获取一个新的长安链地址*/
    @GetMapping("/address/createAll")
    public MessageResult createAll() {
        try {
             chainmakerService.createAll();
            MessageResult result = new MessageResult(200, "success");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return MessageResult.error(500, "MAY rpc error:" + e.getMessage());
        }
    }

    //获取长安链代币余额
    @GetMapping("/balanceOf")
    public MessageResult getAccount(@RequestParam("userId") String userId,@RequestParam("symbol") String symbol) {
        return chainmakerService.balanceOf(userId,symbol);
    }
    //获取长安链代币余额
    @GetMapping("/getBalance")
    public MessageResult getBalance(@RequestParam("address") String address) {
        return chainmakerService.getBalance(address);
    }
    @PostMapping("/transfer")
    public MessageResult transfer(@RequestBody TransferParam transferParam) {
        return chainmakerService.transfer(transferParam);
    }

    @GetMapping("/sync")
    public MessageResult sync() {
        return chainmakerService.sync();
    }


    @GetMapping("/reSync")
    public MessageResult reSync() {
        return chainmakerService.reSync();
    }

    @PostMapping("/transferToAddress")
    public MessageResult transferToAddress(@RequestBody TransferToAddressParam transferToAddressParam) {
        return chainmakerService.transferToAddress(transferToAddressParam);
    }

    @PostMapping("/transferToOwner")
    public MessageResult transferToOwner(@RequestBody TransferToOwnerParam transferParam) {
        return chainmakerService.transferToOwner(transferParam);
    }

    @PostMapping("/transferTo")
    public MessageResult transferTo(@RequestBody TransferToParam transferParam) {
        return chainmakerService.transferTo(transferParam);
    }

    @PostMapping("/addHashCodeByUserId")
    public MessageResult addHashCodeByUserId(@RequestBody AddHashCodeByUserIdParam addHashCodeByUserIdParam) {
        return chainmakerService.addHashCodeByUserId(addHashCodeByUserIdParam);
    }

    @PostMapping("/addHashCodeByAddress")
    public MessageResult addHashCodeByAddress(@RequestBody AddHashCodeByAddressParam addHashCodeByAddressParam) {
        return chainmakerService.addHashCodeByAddress(addHashCodeByAddressParam);
    }

    @GetMapping("/getHashCodeListByAddress")
    public MessageResult getHashCodeListByAddress(@RequestParam("address") String address,@RequestParam("contractName")String contractName) {
        return chainmakerService.getHashCodeList(address,contractName);
    }

    @GetMapping("/getHashCodeListByUserId")
    public MessageResult getHashCodeListByUserId(@RequestParam("userId") Integer userId,@RequestParam("contractName")String contractName) {
        return chainmakerService.getHashCodeListByUserId(userId,contractName);
    }
}
